/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ventasimple.converter;

import com.ventasimple.dao.CategoriaDAO;
import com.ventasimple.dao.CombustibleDAO;
import com.ventasimple.dao.CondicionDAO;
import com.ventasimple.dao.DireccionDAO;
import com.ventasimple.dao.MarcaDAO;
import com.ventasimple.dao.ModeloDAO;
import com.ventasimple.dao.SegmentoDAO;
import com.ventasimple.dao.TarifaDAO;
import com.ventasimple.dao.TipoMotorDAO;
import com.ventasimple.dao.TipoPlanDAO;
import com.ventasimple.dao.TipoPublicacionDAO;
import com.ventasimple.dao.TransmisionDAO;
import com.ventasimple.modelo.Categoria;
import com.ventasimple.modelo.Combustible;
import com.ventasimple.modelo.Condicion;
import com.ventasimple.modelo.Direccion;
import com.ventasimple.modelo.Marca;
import com.ventasimple.modelo.Modelo;
import com.ventasimple.modelo.Segmento;
import com.ventasimple.modelo.Tarifa;
import com.ventasimple.modelo.TipoMotor;
import com.ventasimple.modelo.TipoPlan;
import com.ventasimple.modelo.TipoPublicacion;
import com.ventasimple.modelo.Transmision;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author Claudio
 */
@ManagedBean
@ViewScoped
public class ConverterBean {

    /**
     * Creates a new instance of ConverterBean
     */
    public ConverterBean() {
    }
    
    
    public Converter getTipoPublicacionConverter(){
        
        return new Converter() {

            @Override
            public Object getAsObject(FacesContext context, UIComponent component, String value) {
                if (value.trim().equals("") || value == null) {
                    return null;
                }
                int id = Integer.parseInt(value);
                TipoPublicacionDAO tipoPublicacionDAO = new TipoPublicacionDAO();
                TipoPublicacion r = tipoPublicacionDAO.getTipoPublicacion(id);                
                return r;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {
                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((TipoPublicacion) value).getId().toString() + "";
                }
            }
        };
    }
    
        
    public Converter getMarcaConverter(){
        
        return new Converter() {
            
            @Override
            public Object getAsObject(FacesContext context, UIComponent component,String value) {

                if (value == null || value.equals("")) {
                    return null;
                }
                MarcaDAO marcaDAO = new MarcaDAO();
                Marca m = marcaDAO.getMarca(Integer.parseInt(value));                
                return m;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {

                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((Marca) value).getId()+ "";
                }
            }	
            
        };
    }
    
    public Converter getCategoriaConverter(){
        
        return new Converter() {
            
            @Override
            public Object getAsObject(FacesContext context, UIComponent component,String value) {

                if (value == null || value.equals("")) {
                    return null;
                }
                CategoriaDAO marcaDAO = new CategoriaDAO();
                Categoria m = marcaDAO.getCategoria(Integer.parseInt(value));
                return m;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {

                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((Categoria) value).getId()+ "";
                }
            }            
        };
    }
    
    public Converter getCombustibleConverter(){
        
        return new Converter() {
            
            @Override
            public Object getAsObject(FacesContext context, UIComponent component,String value) {

                if (value == null || value.equals("")) {
                    return null;
                }
                CombustibleDAO marcaDAO = new CombustibleDAO();
                Combustible m = marcaDAO.getCombustible(Integer.parseInt(value));
                return m;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {

                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((Combustible) value).getId()+ "";
                }
            }	
        };
    }
    
    public Converter getCondicionConverter(){
        
        return new Converter() {
            
            @Override
            public Object getAsObject(FacesContext context, UIComponent component,String value) {

                if (value == null || value.equals("")) {
                    return null;
                }
                CondicionDAO marcaDAO = new CondicionDAO();
                Condicion m = marcaDAO.getCondicion(Integer.parseInt(value));
                return m;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {

                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((Condicion) value).getId()+ "";
                }
            }	
        };
    }
    
    public Converter getDireccionConverter(){
        
        return new Converter() {
            
           @Override
    public Object getAsObject(FacesContext context, UIComponent component,String value) {

        if (value == null || value.equals("")) {
            return null;
        }
        DireccionDAO marcaDAO = new DireccionDAO();
        Direccion m = marcaDAO.getDireccion(Integer.parseInt(value));
        return m;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null || value.equals("")) {
            return "";
        } else {
            return ((Direccion) value).getId()+ "";
        }
    }	
        };
    }
    
    public Converter getModeloConverter(){
        
        return new Converter() {
            
           @Override
    public Object getAsObject(FacesContext context, UIComponent component,String value) {

        if (value == null || value.equals("")) {
            return null;
        }
        ModeloDAO modeloDAO = new ModeloDAO();
        Modelo m = modeloDAO.getModelo(Integer.parseInt(value));
        return m;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null || value.equals("")) {
            return "";
        } else {
            return ((Modelo) value).getId()+ "";
        }
    }	
        };
    }
    
    public Converter getSegmentoConverter(){
        
        return new Converter() {
            
           @Override
    public Object getAsObject(FacesContext context, UIComponent component,String value) {

        if (value == null || value.equals("")) {
            return null;
        }
        SegmentoDAO segmentoDAO = new SegmentoDAO();
        Segmento m = segmentoDAO.getSegmento(Integer.parseInt(value));
        return m;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null || value.equals("")) {
            return "";
        } else {
            return ((Segmento) value).getId()+ "";
        }
    }	
        };
    }
    
    public Converter getTipoMotorConverter(){
        
        return new Converter() {
            
           @Override
    public Object getAsObject(FacesContext context, UIComponent component,String value) {

        if (value == null || value.equals("")) {
            return null;
        }
        TipoMotorDAO marcaDAO = new TipoMotorDAO();
        TipoMotor m = marcaDAO.getTipoMotor(Integer.parseInt(value));
        return m;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null || value.equals("")) {
            return "";
        } else {
            return ((TipoMotor) value).getId()+ "";
        }
    }	
        };
    }
    
    public Converter getTipoPlanConverter(){
        
        return new Converter() {
            
           @Override
    public Object getAsObject(FacesContext context, UIComponent component,String value) {

        if (value == null || value.equals("")) {
            return null;
        }
        TipoPlanDAO marcaDAO = new TipoPlanDAO();
        TipoPlan m = marcaDAO.getTipoPlan(Integer.parseInt(value));
        return m;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null || value.equals("")) {
            return "";
        } else {
            return ((TipoPlan) value).getId()+ "";
        }
    }	
        };
    }
    
    public Converter getTransmisionConverter(){
        
        return new Converter() {
            
            @Override
    public Object getAsObject(FacesContext context, UIComponent component,String value) {

        if (value == null || value.equals("")) {
            return null;
        }
        TransmisionDAO marcaDAO = new TransmisionDAO();
        Transmision m = marcaDAO.getTransmision(Integer.parseInt(value));
        return m;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        if (value == null || value.equals("")) {
            return "";
        } else {
            return ((Transmision) value).getId()+ "";
        }
    }	
           
        };
    }
    
    
    public Converter getTarifaConverter(){
        
        return new Converter() {

            @Override
            public Object getAsObject(FacesContext context, UIComponent component, String value) {
                if (value.trim().equals("") || value == null) {
                    return null;
                }
                
                TarifaDAO tarifaDAO = new TarifaDAO();
                Tarifa t = tarifaDAO.getTarifa(Integer.parseInt(value));
                return t;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {
                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((TipoPublicacion) value).getId().toString() + "";
                }
            }
        };
    }
    
    
}
